package br.cin.ufpe.in1118.middleware.distribution.services;

import java.io.Serializable;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class ObjectReference  implements Serializable {

	private static final long serialVersionUID = 3167852555208937070L;

	private String host;

	private int port;

	private Object objectId;

	private String interfaceName;

	public ObjectReference(String host, int port, Object objectId, Class<?> interfac3) {
		this.host = host;
		this.port = port;
		this.objectId = objectId;
		this.interfaceName = interfac3.getCanonicalName();
	}

	public ObjectReference(int port, Object objectId, Class<?> interfac3) {
		this(null, port, objectId, interfac3);
		String hostAddress = "localhost";
		try {
			hostAddress = InetAddress.getLocalHost().getHostAddress();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		this.setHost(hostAddress);
	}

	public String getHost() {
		return this.host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public int getPort() {
		return this.port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public Object getObjectId() {
		return this.objectId;
	}

	public void setObjectId(Object objectId) {
		this.objectId = objectId;
	}

	public String getInterfaceName() {
		return interfaceName;
	}

	public void setInterfaceName(String interfaceName) {
		this.interfaceName = interfaceName;
	}

	@Override
	public boolean equals(Object obj) {
		boolean ret = false;

		if (obj instanceof ObjectReference) {
			ObjectReference reference = (ObjectReference) obj;

			if(reference.host.equalsIgnoreCase(this.host) &&
					reference.port == this.port &&
					reference.objectId.equals(this.objectId) &&
					reference.interfaceName.equalsIgnoreCase(this.interfaceName)){

				ret = true;

			}

		}

		return ret;
	}
	
	@Override
	public String toString() {
		return String.format("ObjectReference[host=\"%s\", port='%05d']", host, port);
	}

}
